/*NODE1*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.rmi.*;
import java.rmi.server.*;
public class Node1 extends JFrame implements ActionListener
{
	private JTabbedPane jTabbedPane1;
	private JPanel contentPane;
	private JTextField jTextField2;
	private JLabel jLabel1,jLabel2,jLabel3;
	private JTextArea jTextArea1;
	private JScrollPane jScrollPane2;
	private JTextArea jTextArea2;
	private JScrollPane jScrollPane3;
	private JButton jButton1;
	private JButton jButton2;
	private JButton jButton3;
	private JButton jButton4;
	private JButton jButton5;
	private JButton jButton6;
	private JPanel jPanel1;
	private JComboBox jComboBox1;
	private JPanel jPanel2;
	private int i,m,n,k=0,length1,value,cond=0;
	long time,d;
	String pac[];
	

	String s;
	String message;
	Socket client1;
	Socket S1;	
	ServerSocket server1;
	
		
	public Node1()
	{
		super();
		initializeComponent();
		this.setVisible(true);
		this.setBackground(new Color(153, 0, 51));
		try
		{
			server1=new ServerSocket(11);			

		}	
				
		catch (Exception exp)
		{
			exp.printStackTrace();
		}
	}

	
	
	private void initializeComponent()
	{
		jTabbedPane1 = new JTabbedPane();
		contentPane = (JPanel)this.getContentPane();
		jTextField2 = new JTextField();
		jTextArea1 = new JTextArea();
		jScrollPane2 = new JScrollPane();
		jTextArea2 = new JTextArea();
		jScrollPane3 = new JScrollPane();
		jTextArea1.setFont(new Font("Arial",Font.BOLD,12));
		jTextArea2.setFont(new Font("Arial",Font.BOLD,12));

		jButton1 = new JButton();
		jButton1.setFont(new Font("Arial",Font.BOLD,12));
		jButton1.setBackground(new java.awt.Color(100, 204, 255));
		jButton2 = new JButton();
		jButton2.setFont(new Font("Arial",Font.BOLD,12));
		jButton2.setBackground(new java.awt.Color(100, 204, 255));
		jButton3 = new JButton();
		jButton3.setFont(new Font("Arial",Font.BOLD,12));
		jButton3.setBackground(new java.awt.Color(100, 204, 255));
		jButton4 = new JButton();
		jButton4.setFont(new Font("Arial",Font.BOLD,12));
		jButton4.setBackground(new java.awt.Color(100, 204, 255));
		jButton5 = new JButton();
		jButton5.setFont(new Font("Arial",Font.BOLD,12));
		jButton5.setBackground(new java.awt.Color(100, 204, 255));
		jButton6 = new JButton();
		jButton6.setFont(new Font("Arial",Font.BOLD,12));
		jButton6.setBackground(new java.awt.Color(100, 204, 255));
		jPanel1 = new JPanel();
		jComboBox1 = new JComboBox();
		jComboBox1.addActionListener(this);
		jComboBox1.addItem("");
        jComboBox1.addItem("Node2");
        jComboBox1.addItem("Node3");
		jComboBox1.addItem("Node4");
		//jComboBox1.addItem("Node5");
        
		jLabel2 = new JLabel();
	    jLabel2.setFont(new java.awt.Font("Arial", 1, 20));
        jLabel2.setForeground(new java.awt.Color(153, 0, 100));
        jLabel2.setText("Bandwidth Recycling in IEEE 802.16 Networks");
        add(jLabel2);
        jLabel2.setBounds(40, 10, 500, 30);
		
		jLabel3 = new JLabel();
	    jLabel3.setFont(new java.awt.Font("Arial", 1, 20));
        jLabel3.setForeground(new java.awt.Color(200, 0, 100));
        jLabel3.setText("NODE 1");
        add(jLabel3);
        jLabel3.setBounds(350, 40, 500, 30);
		
		jLabel1 = new JLabel();
	    jLabel1.setFont(new Font("Arial",Font.BOLD,12));				
		jPanel2 = new JPanel();
		jTabbedPane1.addTab("   Node1   ", jPanel1);
        jTabbedPane1.setFont(new Font("Arial",Font.BOLD,13));
		jTabbedPane1.addTab("Received Message", jPanel2);
		jTabbedPane1.setFont(new Font("Arial",Font.BOLD,13));
		
		
		jTabbedPane1.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e)
			{
				jTabbedPane1_stateChanged(e);
			}

		});
		
		
		
		
		jLabel1.setText("Destination Node :");
		jLabel1.setForeground(new java.awt.Color(153, 0, 100));
		contentPane.setLayout(null);

		addComponent(contentPane, jTabbedPane1, 55,49,523,433);		
		
			
			jTextField2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				jTextField2_actionPerformed(e);
			}

		});
		jTextArea1.setText("");
		jScrollPane2.setViewportView(jTextArea1);		
		jScrollPane3.setViewportView(jTextArea2);
		jButton1.setText("Browse");
		jButton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				jButton1_actionPerformed(e);
			}

		});

		jButton2.setText("RREQ");
		jButton2.addActionListener(this);
		jButton3.setText("Exit");
		jButton3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				jButton3_actionPerformed(e);
			}

		});
		jButton5.setText("SEND");
		  jButton5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				jButton5_actionPerformed(e);
			}

		});
		jButton6.setText("CLEAR");
		  jButton6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				jButton6_actionPerformed(e);
			}

		});
		
		jButton4.setText("Exit");
		  jButton4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				jButton4_actionPerformed(e);
			}

		});
	
		jPanel1.setLayout(null);
		addComponent(jPanel1, jTextField2, 19,144,223,22);
		addComponent(jPanel1, jScrollPane2, 21,195,220,193);
		addComponent(jPanel1, jButton1, 266,140,78,28);
		addComponent(jPanel1, jButton2, 267,200,71,28);
		addComponent(jPanel1, jComboBox1, 130,52,100,22);
		addComponent(jPanel1, jLabel1,  22,52,112,18);
		addComponent(jPanel1,jButton5,267,250,71,28);
		addComponent(jPanel1,jButton6,267,300,72,28);
        addComponent(jPanel1, jButton3, 267,360,71,28);
		  

		jPanel2.setLayout(null);
		
		addComponent(jPanel2, jScrollPane3, 80,50,343,295);
		addComponent(jPanel2,jButton4,208,360,83,28);
		
		
		this.setTitle("Bandwidth Recycling in IEEE 802.16 Networks ");
		
        
		 
		
		jPanel2.setBackground(new Color(111, 100, 100));
		jPanel1.setBackground(new Color(111, 100, 100));
		contentPane.setBackground(new Color(220, 250, 220));
		this.setLocation(new Point(0, 0));
		this.setSize(new Dimension(600, 534));
	}

	private void addComponent(Container container,Component c,int x,int y,int width,int height)
	{
		c.setBounds(x,y,width,height);
		container.add(c);
	}
    private void jComboBox1_actionPerformed(ActionEvent e)
			{
				System.out.println("\njComboBox1_actionPerformed(ActionEvent e) called.");
				Object o = jComboBox1.getSelectedItem();
				System.out.println(">>" + ((o==null)? "null" : o.toString()) + " is selected.");
			}
	
	
	private void jTabbedPane1_stateChanged(ChangeEvent e)
	{
		System.out.println("\njTabbedPane1_stateChanged(ChangeEvent e) called.");

	}

	private void jTextField2_actionPerformed(ActionEvent e)
	{
		System.out.println("\njTextField2_actionPerformed(ActionEvent e) called.");

	}	
	
	
	private void jButton1_actionPerformed(ActionEvent e)
	{
		System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");
		try
		{
			s="";
			int b;
			FileDialog fd=new FileDialog(this,"Open",FileDialog.LOAD);
			fd.show();
			FileInputStream fis=new FileInputStream(fd.getDirectory()+fd.getFile());
			jTextField2.setText(fd.getDirectory()+fd.getFile());
			
			
					
			while((b=fis.read())!=-1)
			{
				message+=(char)b;
			}
			jTextArea1.setText(message);
	

	     }	
		 catch(Exception r)
			{
				r.printStackTrace();
			}   
		
	}
	
	
	
	public void actionPerformed(ActionEvent e)				
	{ 
		
		String valu="";
		String check=""+jComboBox1.getSelectedItem();
		
		if((e.getSource())==jButton2)
		{
			cond=1;
				try
				{
					Interface is=(Interface)Naming.lookup("//localhost/Server");		
					value=is.retrieve1(check);
				}
				catch (Exception ew)
				{
					ew.printStackTrace();
				}					
		 }
    }
					
                        
		
	

	
	
	private void jButton3_actionPerformed(ActionEvent e)
	{
		this.dispose();
	
	}
	
	
	
	
	private void jButton4_actionPerformed(ActionEvent e)
	{		
		this.dispose();	
	}


	private void jButton6_actionPerformed(ActionEvent e)
	{		
		jTextArea1.setText("");	
	}
	
	
	
	private void jButton5_actionPerformed(ActionEvent e)
	{
		int chk=0;
		try
	  {
	 if(cond==1)
	   {
		
			if(jComboBox1.getSelectedItem().equals("Node2"))
			 {
				JOptionPane.showMessageDialog(this,"Message send to Node2");
				m=22;
				n=22;

			 }
			else if(jComboBox1.getSelectedItem().equals("Node3"))
			{
				JOptionPane.showMessageDialog(this,"Message send to Node3");
				m=33;
				n=33;
			
			}
			else if(jComboBox1.getSelectedItem().equals("Node4"))
			{
				
					if(value==0)
					{
						m=22;
						n=44;
						JOptionPane.showMessageDialog(this,"Message send to Node2");
					}
					else
					{
						m=33;
						n=44;
						JOptionPane.showMessageDialog(this,"Message send to Node3");
					}
			}
			else if(jComboBox1.getSelectedItem().equals("Node5"))
			{
				JOptionPane.showMessageDialog(this,"Message send to Node5");
				m=55;					
			}



			client1=new Socket("localhost",m);
			DataOutputStream out=new DataOutputStream(client1.getOutputStream());			
			if(out!=null)
			{
				out.flush();
			}
				time=System.currentTimeMillis();
				int b=11;
				out.writeInt(b);				
				out.writeInt(n);
				out.writeLong(time);
				int outgoing=0;
				byte buffer[]=message.getBytes();
				int len=buffer.length;
				int tlength=buffer.length/48;
				int length11=buffer.length%48;
				int len1=len;
				if(length11!=0)
				{
					tlength++;
				}
				out.writeInt(tlength);
				
				
				
				int st=0;
				int end=48;           		
				
				System.out.println("Message Length:    "+len);
				String prefinal="tytyt";
				String finalst="";
						if (len<=48)
							 {
								 finalst=prefinal+message+"\n"+"null";
								 System.out.println("Message: "+message);
								 System.out.println("Out going Packets: "+(++outgoing));
								 out.writeUTF(message); 

							 }
							 else
							  {
								
								System.out.println("Message: "+message.substring(st,end));
								System.out.println("Out going Packets: "+(++outgoing));
								out.writeUTF(message.substring(st,end));											
								Thread.sleep(1000);
								while(len1>48)
								  {
									 len1-=48;
									 if(len1<=48)
									   {
									 
										 System.out.println("Message: "+message.substring(end,len));
										 System.out.println("Out going Packets: "+(++outgoing));
										 out.writeUTF(message.substring(end,len));						  
									  		
									   }
									  else
									   {
										    int sp=end+48;
											System.out.println("Message: "+message.substring(end,sp));
											System.out.println("Out going Packets: "+(++outgoing));
											out.writeUTF(message.substring(end,sp));											
											end=sp;										
											
									   }
								    }								  
							    }
								  String del="Data Transmission is completed";		
								  JOptionPane.showMessageDialog(this,del);
							      Interface i=(Interface)Naming.lookup("//localhost/Server");				  
								  chk=i.delete();
								  Thread.sleep(1500);
								  if(chk==0)
										{														  
											System.out.println("database items are deleted");
										}
								  long flen=message.length();
								  out.writeLong(flen);
								  --cond;
								}
							else
							{
							  String check="confirm the RREQ";
							  JOptionPane.showMessageDialog(this,check);	
							}
						  }
						  

						catch (Exception exp)
						{
							exp.printStackTrace();
						}

						
					}




public void Server()
	{
				try
					{
						int count=0;
						String rr[];
						S1=server1.accept();	
						DataInputStream dis=new DataInputStream(S1.getInputStream());
						int source=dis.readInt();
						jTextArea1.append("Source Address  "+source+"\n");
						int s=dis.readInt();
						jTextArea1.append("Destination Address  "+s+"\n");
						if(s==11)
						 {
							long sender_time=dis.readLong();
							int length=dis.readInt();							
							rr=new String[length];
							while(length>0)
							{								
							   rr[count]=dis.readUTF();
							   System.out.println("the received output is"+rr[count]);
							   jTextArea2.append("\n\nPacket  "  +  "-->\n***********\n"+rr[count]);															
							   length--;
							   count++;
							}
							 long d=dis.readLong();								 				 
							 double flen=(double)d/1024;
							 long receiver_time=System.currentTimeMillis();							
							 double delay=((double)(receiver_time-sender_time)/1000);
							 jTextArea1.append("delay   " +delay+ "\n");
							 String sd=""+flen;
							 jTextArea1.append(("file length"+"  "+sd.substring(0,4)+" KB \n"));
							 double Bandwidth=(double)flen/delay;
							 String as=""+Bandwidth;								 
							 jTextArea1.append("Bandwidth"+"  "+as.substring(0,5)+"kbps \n");	

						 }
						 else
							 {
								if(s==33)
								 {
									jTextArea1.append("MESSAGE SEND TO NODE3 \n");
									String pro="RREQ to NODE3";									
								    JOptionPane.showMessageDialog(this,pro);
									Thread.sleep(500);

									String pro1="RREP From NODE3";	
									JOptionPane.showMessageDialog(this,pro1);
								 }
								 else
								 {
									 jTextArea1.append("MESSAGE SEND TO NODE2 \n");
									 String pro="RREQ to NODE2";
									 JOptionPane.showMessageDialog(this,pro);
									 Thread.sleep(500);

									 String pro1="RREP From NODE2";	
									 JOptionPane.showMessageDialog(this,pro1);
							     }
								long sender_time=dis.readLong();
								int length=dis.readInt();
								int c=length;
								pac=new String[length];
								System.out.println("length===="+length);																
							    while(length>0)
									{
									   pac[count]=dis.readUTF();
									   jTextArea2.append("\n\nPacket  " + "-->\n***********\n"+pac[count]);															
									   length--;	
									   count++;
									}
									d=dis.readLong();
									Socket client4=new Socket("localhost",s);
									DataOutputStream dout=new DataOutputStream(client4.getOutputStream());
									dout.writeInt(source);
									dout.writeInt(s);									
									dout.writeLong(sender_time);
									System.out.println(""+sender_time);									
									dout.writeInt(c);
									for(int q=0;q<count;q++)
								     {										
									    dout.writeUTF(pac[q]);
								     }
									dout.writeLong(d);

							 }												
					 }
				    catch (Exception exp)
							{
								exp.printStackTrace();
							}
													
						}			

 
public static void main(String[] args)
	{		
		Node1 node =new Node1();
		while(true)
		{
		
			node.Server();
			
		}

	}	
}